#include "control.h"

#define KEY_PLAY_PIN GET_PIN(E,3)
#define KEY_LAST_PIN GET_PIN(E,2)
#define KEY_NEXT_PIN GET_PIN(E,4)
#define KEY_PRESS_LEVEL 0

static struct my_key key_last={0},key_next={0},key_play={0};

extern struct player player;

static void beep_key_press_long(rt_uint32_t pin)
{
    uint8_t volume;
    switch (pin)
    {
    case KEY_PLAY_PIN:
        break;
    case KEY_LAST_PIN:
        player_control(&player, PLAYER_CMD_GET_VOL, &volume);
        if (volume > 1)
        {
            volume--;
            player_control(&player, PLAYER_CMD_SET_VOL, &volume);
        }
        break;
    case KEY_NEXT_PIN:
        player_control(&player, PLAYER_CMD_GET_VOL, &volume);
        if (volume < 99)
        {
            volume++;
            player_control(&player, PLAYER_CMD_SET_VOL, &volume);
        }
        break;
    default:
        ;
    }
}

static void beep_key_press_short(rt_uint32_t pin)
{
    switch(pin)
    {
    case KEY_PLAY_PIN:
        if(player.status==PLAYER_RUNNING)
        {
            player_control(&player, PLAYER_CMD_STOP, RT_NULL);
        }
        else {
            player_control(&player, PLAYER_CMD_PLAY, RT_NULL);
        }
        player_show(&player);
        break;
    case KEY_LAST_PIN:
        player_control(&player, PLAYER_CMD_LAST, RT_NULL);
        player_show(&player);
        break;
    case KEY_NEXT_PIN:
        player_control(&player, PLAYER_CMD_NEXT, RT_NULL);
        player_show(&player);
        break;
    }
}



void key_cb(struct my_key *key)
{
    switch(key->event)
    {
    case KEY_EVENT_CLICK_UP:
        beep_key_press_short(key->pin);
        rt_kprintf("1\n");
        break;
    case KEY_EVENT_HOLD_CYC:
        beep_key_press_long(key->pin);
        rt_kprintf("2\n");
        break;
    default:
        ;
    }
}

int control_init(void)
{
    key_last.press_logic_level = KEY_PRESS_LEVEL;
    key_last.hold_cyc_period=100;
    key_last.cb = (my_key_callback)key_cb;

    key_next = key_play = key_last;

    key_last.pin = KEY_LAST_PIN;
    key_next.pin = KEY_NEXT_PIN;
    key_play.pin = KEY_PLAY_PIN;

    my_key_register(&key_last);
    my_key_register(&key_play);
    my_key_register(&key_next);

    my_key_start();

    return 0;

}





